package com.aos.service.permission.impl;

import com.aos.dao.PermissionDao;
import com.aos.dao.RolePermissionDao;
import com.aos.dto.ApplicationDto;
import com.aos.dto.PermissionDto;
import com.aos.entity.Permission;
import com.aos.entity.User;
import com.aos.service.permission.PermissionService;
import com.aos.util.CopyUtils;
import com.aos.util.Result;
import com.aos.util.TreeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @ClassName PermissionServiceImpl
 * @Description:页面资源
 * @Author wumingwang
 * @Date 2018/9/9 12:41
 * @Version 1.0
 */
@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    public Result queryByPage(Map<String, Object> map) {
        /**开启分页查询，sql的执行要紧跟这个分页工具*/
        PageHelper.startPage((int)map.get("pageNum"),(int)map.get("pageSize"));
        List<PermissionDto> list =  permissionDao.queryByPage(map);
        PageInfo<PermissionDto> p = new PageInfo<>(list);
        return Result.ok(p);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addPermission(Permission permission) {
        PermissionDto pdo = permissionDao.queryForCheckByCodeAndAppId(permission);
        if(pdo != null){
            return Result.error("应用："+pdo.getAppName()+"下该权限码已存在！");
        }
        if(permission.getParentId() == null){
            permission.setParentId(0L);
        }
        permissionDao.insert(permission);
        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteById(Long id) {
        permissionDao.delete(id);
        /**删除该菜单对应的角色关系*/
        rolePermissionDao.deleteByPermissionId(id);
        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updatePermission(Permission permission) {
        PermissionDto pdo = permissionDao.queryForCheckByCodeAndAppId(permission);
        if(pdo != null){
            return Result.error("应用："+pdo.getAppName()+"下该权限码已存在！");
        }
        Permission oldpermission = permissionDao.queryById(permission.getId());
        if(oldpermission == null){
            return Result.error("要更新的菜单不存在！");
        }
        try{
            CopyUtils.copy(permission,oldpermission);
        }catch (Exception e){
            log.error("属性复制错误！",e);
            return Result.error("系统繁忙，请稍后再试！");
        }
        if(permission.getParentId() == null){
            oldpermission.setParentId(0L);
        }
        permissionDao.update(oldpermission);
        return Result.ok();
    }

    @Override
    public Result queryDetails(Long id) {
        return Result.ok(permissionDao.queryDetailsById(id));
    }

    @Override
    public Result queryListByAppId(Long applicationId) {
        List<PermissionDto> list = permissionDao.queryListByAppIdAndRoleId(applicationId,null);
        return Result.ok(TreeUtils.buildTree(list));
    }

    @Override
    public Result queryMenuListByAppId(Long applicationId) {
        return Result.ok(permissionDao.queryListByAppId(applicationId,2));
    }
}
